﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace DTW_Beta
{
    static class ZetServerConnection
    {
        public static bool IS_SERVER_ON = false;
        private static DateTime lastSentTime = DateTime.MinValue;
        private static WebClient webClient = new WebClient();

        private static string ConvertHex(int number)
        {
            string temp = number.ToString("X");

            temp = (temp.Length == 1) ? "0" + temp : temp;

            return temp;
        }

        private static void MakeRequest(string url)
        {
            if (IS_SERVER_ON)
            {
                try
                {
                    TimeSpan diff = DateTime.Now - lastSentTime;

                    if (diff.TotalMilliseconds > 20)
                    {
                        Uri temp = new Uri(url);
                        webClient.UploadStringAsync(temp, "POST", "");

                        lastSentTime = DateTime.Now;
                    }
                }
                catch { }
            }
        }

        public static void IsikAc()
        {
            MakeRequest("http://192.168.2.249/knx/admin.php?addr=0/3/0&dtype=L&value=FF");
        }

        public static void IsikKapat()
        {
            MakeRequest("http://192.168.2.249/knx/admin.php?addr=0/3/0&dtype=L&value=00");
        }

        public static void PerdeAc()
        {
            MakeRequest("http://192.168.2.249/knx/admin.php?addr=0/0/34&dtype=S&value=00");
        }

        public static void PerdeKapat()
        {
            MakeRequest("http://192.168.2.249/knx/admin.php?addr=0/0/34&dtype=S&value=01");
        }

        public static void IsikAyarla(int level)
        {
            string hex = ConvertHex(level);

            MakeRequest("http://192.168.2.249/knx/admin.php?addr=0/3/0&dtype=L&value=" + hex);
        }
    }
}
